/*****************************************************************************************
 * Dexter : Distributed Experimental and Tester Platform
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Configuration {
	
	private static String configXML = "dexter-config.xml";
	private static Properties config;
	
	private static void init() {
		if(config==null) {
			try {
				config = loadProps(configXML);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Properties loadProps(String path) throws IOException {
		
		FileInputStream fis = new FileInputStream(path);
		String ext = path.substring(path.lastIndexOf('.')+1);
		Properties prop = new Properties();
		if (ext.equals("xml")) {
			prop.loadFromXML(fis);
		}
		else prop.load(fis);
		fis.close();
		return prop;
	}
	
	/**** FRONT-end ****/
	
	public static String getFrontHostName() {
		init();
		return config.getProperty("front_host");
	}
	
	public static int getFrontPort() {
		init();
		return Integer.parseInt(config.getProperty("front_port"));
	}

	/**** COORDinator ****/
	
	public static String getCoordHostName() {
		init();
		return config.getProperty("coord_host");
	}
	
	public static int getCoordPort() {
		init();
		return Integer.parseInt(config.getProperty("coord_port"));
	}
	
	public static int getSensorPeriod() {
		init();
		int min = Integer.parseInt(config.getProperty("sensor_period"));
		return min*60*1000;
	}
	
	public static String getDatabaseDriver() {
		init();
		return config.getProperty("db_driver");
	}
	
	public static String getDatabaseURL() {
		init();
		return config.getProperty("db_url");
	}
	
	public static String getDatabaseUser() {
		init();
		return config.getProperty("db_user");
	}
	
	public static String getDatabasePassword() {
		init();
		return config.getProperty("db_pwd");
	}
	
	

}
